#!/usr/bin/env bash
CXX=g++
CFLAGS=-std=c++11
OUTPUT=$1
if test -z "$OUTPUT"; then
  echo "usage: $0 <output-filename>" >&2
  exit 1
fi

# we depend on C++11
PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS"

if test -z "$TARGET_OS"; then
    TARGET_OS=`uname -s`
fi

# On GCC, we pick libc's memcmp over GCC's memcmp via -fno-builtin-memcmp
case "$TARGET_OS" in
    Darwin)
        # PORT_FILES=port/darwin/darwin_specific.cc
        ;;
    Linux)
        if [ -z "$USE_CLANG" ]; then
            COMMON_FLAGS="$COMMON_FLAGS -fno-builtin-memcmp"
        else
            PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -latomic"
        fi
        PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lpthread -lrt"
        ;;
    SunOS)
        PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lpthread -lrt -static-libstdc++ -static-libgcc -m64"
        ;;
    AIX)
        PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -pthread -lpthread -lrt -maix64 -static-libstdc++ -static-libgcc"
        ;;
    FreeBSD)
        PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lpthread"
        ;;
    NetBSD)
        PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lpthread -lgcc_s"
        ;;
    OpenBSD)
        PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -pthread"
        ;;
    DragonFly)
        PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lpthread"
        ;;
    Cygwin)
        if [ -z "$USE_CLANG" ]; then
            COMMON_FLAGS="$COMMON_FLAGS -fno-builtin-memcmp"
        else
            PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -latomic"
        fi
        PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lpthread -lrt"
        # PORT_FILES=port/linux/linux_specific.cc
        ;;
    *)
        echo "Unknown platform!" >&2
        exit 1
esac

    if ! test $ROCKSDB_DISABLE_SNAPPY; then
        # Test whether Snappy library is installed
        # http://code.google.com/p/snappy/
        $CXX $CFLAGS -x c++ - -o /dev/null 2>/dev/null  <<EOF
          #include <snappy.h>
          int main() {}
EOF
        if [ "$?" = 0 ]; then
            PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lsnappy"
        fi
    fi

    if ! test $ROCKSDB_DISABLE_GFLAGS; then
        # Test whether gflags library is installed
        # http://gflags.github.io/gflags/
        # check if the namespace is gflags
        $CXX $CFLAGS -x c++ - -o /dev/null 2>/dev/null << EOF
          #include <gflags/gflags.h>
          int main() {}
EOF
        if [ "$?" = 0 ]; then
            PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lgflags"
        else
          # check if namespace is google
          $CXX $CFLAGS -x c++ - -o /dev/null 2>/dev/null << EOF
            #include <gflags/gflags.h>
            using namespace google;
            int main() {}
EOF
          if [ "$?" = 0 ]; then
              PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lgflags"
          fi
        fi
    fi

    if ! test $ROCKSDB_DISABLE_ZLIB; then
        # Test whether zlib library is installed
        $CXX $CFLAGS $COMMON_FLAGS -x c++ - -o /dev/null 2>/dev/null  <<EOF
          #include <zlib.h>
          int main() {}
EOF
        if [ "$?" = 0 ]; then
            PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lz"
        fi
    fi

    if ! test $ROCKSDB_DISABLE_BZIP; then
        # Test whether bzip library is installed
        $CXX $CFLAGS $COMMON_FLAGS -x c++ - -o /dev/null 2>/dev/null  <<EOF
          #include <bzlib.h>
          int main() {}
EOF
        if [ "$?" = 0 ]; then
            PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lbz2"
        fi
    fi

    if ! test $ROCKSDB_DISABLE_LZ4; then
        # Test whether lz4 library is installed
        $CXX $CFLAGS $COMMON_FLAGS -x c++ - -o /dev/null 2>/dev/null  <<EOF
          #include <lz4.h>
          #include <lz4hc.h>
          int main() {}
EOF
        if [ "$?" = 0 ]; then
            PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -llz4"
        fi
    fi

    if ! test $ROCKSDB_DISABLE_ZSTD; then
        # Test whether zstd library is installed
        $CXX $CFLAGS $COMMON_FLAGS -x c++ - -o /dev/null 2>/dev/null  <<EOF
          #include <zstd.h>
          int main() {}
EOF
        if [ "$?" = 0 ]; then
            COMMON_FLAGS="$COMMON_FLAGS -DZSTD"
            PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lzstd"
        fi
    fi

echo "PLATFORM_LDFLAGS=$PLATFORM_LDFLAGS" > "$OUTPUT"
